#include "widget.h"


#include <QHBoxLayout>
#include <QVBoxLayout>


#include <QFileDialog>
#include <QDebug>
#include <QColor>
#include <QColorDialog>
#include <QFont>
#include <QFontDialog>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    label = new QLabel(this);
    label->setMinimumSize(200, 100);
    pushButtonFile = new QPushButton("打开文件", this);
    pushButtonFiles = new QPushButton("打开文件集", this);
    pushButtonSaveFile = new QPushButton("保存文件", this);
    pushButtonColor = new QPushButton("颜色", this);
    pushButtonFont = new QPushButton("字体", this);


    QHBoxLayout *hbox = new QHBoxLayout();
    hbox->addWidget(pushButtonFile);
    hbox->addWidget(pushButtonFiles);
    hbox->addWidget(pushButtonSaveFile);
    hbox->addWidget(pushButtonColor);
    hbox->addWidget(pushButtonFont);


    QVBoxLayout *vbox = new QVBoxLayout();
    vbox->addWidget(label);
    vbox->addLayout(hbox);
    this->setLayout(vbox);


    connect(pushButtonFile, &QPushButton::clicked, this, &Widget::getFileName);
    connect(pushButtonFiles, &QPushButton::clicked, this, &Widget::getFileNames);
    connect(pushButtonSaveFile, &QPushButton::clicked, this, &Widget::SaveFileName);
    connect(pushButtonColor, &QPushButton::clicked, this, &Widget::setColor);
    connect(pushButtonFont, &QPushButton::clicked, this, &Widget::mysetFont);


}


Widget::~Widget()
{


}


void Widget::getFileName()
{
    /* 打开文件对话框 */
    QString filename = QFileDialog::getOpenFileName(this, tr("Open File"),
                                 ".",
                                 tr("Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"));
    label->setText(filename);
}


void Widget::getFileNames()
{
    /* 打开文件列表对话框  */


    QStringList files = QFileDialog::getOpenFileNames(
                              this,
                              "Select one or more files to open",
                              "/home",
                              "Images (*.png *.xpm *.jpg);;All (*.*)");
    for (int i = 0; i < files.size(); i++) {
        qDebug() << files.at(i);
    }
}


void Widget::SaveFileName()
{
    /* 保存文件对话框 */
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
                                 ".",
                                 tr("Images (*.png *.xpm *.jpg)"));

//    QFileDialog::getOpenFileUrl()
}


void Widget::setColor()
{
    /* 颜色对话框 */
    QColor color = QColorDialog::getColor();
    qDebug() << color.red() << " " << color.green() << " " << color.blue() << endl;
}


void Widget::mysetFont()
{
    /* 字体对话框 */
    bool ok;
    QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this);
    if (ok) {
         // font is set to the font the user selected
        qDebug() << font.family() << " " << font.pointSize();
    } else {
         // the user canceled the dialog; font is set to the initial
         // value, in this case Times, 12.
    }
}
